package com.ec.base;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * Created by VincentZee on 2019/1/31
 */
@Component
public class BaseRedisService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public void setString(String key, Object data){
        setString(key, data, null);
    }

    public void setString(String key, Object data, Long timeout){
        if(data instanceof String){
            String value = data.toString();
            stringRedisTemplate.opsForValue().set(key, value);
        }
        if(timeout != null){
            stringRedisTemplate.expire(key, timeout, TimeUnit.SECONDS);
        }
    }

    public String getString(String key){
        return stringRedisTemplate.opsForValue().get(key);
    }

    public void delKey(String key){
        stringRedisTemplate.delete(key);
    }
}
